package com.cema.manage.project.android.biz;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.cema.manage.project.android.TestBaseContrller;
import com.cema.manage.project.android.result.JsonResult;
import com.cema.manage.project.android.service.BizVersionUpdateService;
import com.cema.manage.project.manage.users.domain.Users;
import com.cema.manage.project.manage.versionUpdate.domain.VersionUpdate;
import com.cema.manage.redis.RedisService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("version_update")
public class BizVersionUpdateController extends TestBaseContrller {
    @Resource
    private BizVersionUpdateService bizVersionUpdateService;

    @Resource
    private RedisService redisService;

    @PostMapping("version")
    public JsonResult getVersion(HttpServletRequest request) {
        Users user = getUser(request);
        String uvChannel = request.getParameter("uvChannel");
        if (StrUtil.isEmpty(uvChannel)) {
            return JsonResult.returnResetLogin("登录失效、请重新登录");
        }
        VersionUpdate versionUpdate = bizVersionUpdateService.getVersion(Integer.parseInt(uvChannel));
        if (ObjectUtil.isNull(versionUpdate)) {
            return JsonResult.returnResetLogin("登录失效、请重新登录");
        }
        if (ObjectUtil.isNotNull(user) && StrUtil.isEmpty(user.getOaid())) {
            redisService.remove(user.getDeviceId());
        }
        return JsonResult.returnResult(JsonResult.SUCCESS, versionUpdate);
    }
}
